<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php include('menu.php'); ?>
</head>
<body>
<?php
include("../connect/connect.php");

//ลบข้อมูล
if($_GET["action"]==del) {

	$Query = "DELETE FROM product WHERE product_id = '".$_GET["id"]."' ";
	$objQuery = mysql_query($Query);
	
	if(!$objQuery)
	{
		echo "Error Delete [".mysql_error()."]";
	} else {
		echo "<script>window.location = 'product_manage.php';</script>";
	}
}
//ค้นหาสินค้า
if($_POST["code_search"] != "" OR $_POST["name_search"] != "" OR $_POST["category_search"] != "" OR $_POST["cost_search"] != "" OR $_POST["price_search"] != "") {
	
		$Query = "SELECT * FROM product,category WHERE product.category_id = category.category_id ";
	
	if($_POST["code_search"] != "") {
		$Query .= "AND code LIKE '%".$_POST["code_search"]."%' ";
	}
	if($_POST["name_search"] != "") {
		$Query .= "AND name LIKE '%".$_POST["name_search"]."%' ";
	}
	if($_POST["category_search"] != "") {
		$Query .= "AND category.category_id = '".$_POST["category_search"]."' ";
	}
	if($_POST["cost_search"] != "") {
		$Query .= "AND cost LIKE '%".$_POST["cost_search"]."%' ";
	}
	if($_POST["price_search"] != "") {
		$Query .= "AND price LIKE '%".$_POST["price_search"]."%' ";
	}
	
	$Query .= " ORDER BY category.category_id,code_num";
	
} else {
	$Query = "SELECT * FROM product,category WHERE product.category_id = category.category_id ORDER BY category.category_id,code_num";
}
$cQuery = mysql_query("SELECT * FROM category");
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>จัดการสินค้า</h1>
    <div class="buttons"><a onclick="window.location='product_add.php';" class="button"><span>เพิ่มสินค้า</span></a></div></div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
            <td width="1" style="text-align: center;"><span class="center">รหัสสินค้า</span></td>
            <td class="center">ชื่อสินค้า</td>
            <td class="center"  width="15%">หมวดสินค้า</td>
            <?php if($_SESSION['user_group_id']!=0) { ?><td class="center"  width="15%">ต้นทุน</td><?php } ?>
            <td class="center"  width="15%">ราคาขาย</td>
            <td class="center"  width="10%">จุดสั่งซื้อ</td>
            <?php if($_SESSION['user_group_id']!=0) { ?><td class="center"  width="10%">จัดการสินค้า</td><?php } ?>
          </tr>
        </thead>
        <tbody>
		<form action="<?php $_SERVER["PHP_SELF"];?>" name="formsearch" method="post" id="formsearch">
          <tr class="filter">
            <td><input name="code_search" type="text" id="code_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><input name="name_search" type="text" id="name_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><select name="category_search" >
            		<option value=""><--ชื่อหมวดสินค้า--></option>
            	<?php while($cRow=mysql_fetch_array($cQuery)){ ?>
            		<option value="<?php echo $cRow['category_id']; ?>"><?php echo $cRow['category_name']; ?></option>
            	<?php } ?>
            </select></td>
            <?php if($_SESSION['user_group_id']!=0) { ?><td align="center"><input name="cost_search" type="text" id="cost_search" style="text-align: center;" size="7" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td><?php } ?>
            <td align="center"><input name="price_search" type="text" id="price_search" style="text-align: center;" size="7" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><?php if($_SESSION['user_group_id']=='0') { ?><a onclick="formsearch.submit();" class="button"><span>ค้นหา</span></a><?php } ?></td>
            <?php if($_SESSION['user_group_id']!=0) { ?><td align="center"><a onclick="formsearch.submit();" class="button"><span>ค้นหา</span></a></td><?php } ?>
          </tr>
		</form>
<?php
	while($row = mysql_fetch_array($objQuery))
		{
?>
          <tr>
            <td class="right" style="padding-right:15px"><?php echo $row["code"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["name"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["category_name"];?></td>
            <?php if($_SESSION['user_group_id']!=0) { ?>    <td class="right" style="padding-right:30px"><?php echo $row["cost"];?></td> <?php } ?>
         	<td class="right" style="padding-right:30px"><?php echo $row["price"];?></td>
            <td class="right" style="padding-right:30px"><?php echo $row["rop"];?></td>
        <?php if($_SESSION['user_group_id']!=0) { ?> <td class="center">[ <a href="product_edit.php?id=<?php echo $row["product_id"];?>">แก้ไข</a> ] [ <a href="JavaScript:if(confirm('คุณต้องการยืนยันการลบข้อมูล ใช่หรือไม่?')==true){window.location='<?php echo $_SERVER["PHP_SELF"];?>?action=del&id=<?php echo $row["product_id"];?>';}">ลบ</a> ]</td><?php } ?>
          </tr>
<?php
		} 
?>
        </tbody>
      </table>
  </div>
</div>

</body>
<?php mysql_close(); ?>
</html>
